#include <jni.h>
#include <string>
#include <libgen.h>
#include "tftp/logcat.h"

extern "C" {
#include "tftp/program.h"
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_xuexiang_tftp_TFTPClientUtils_download(JNIEnv *env, jclass type, jstring hostAddress_,
                                           jstring remoteFile_, jstring localFile_) {
    const char *hostAddress = env->GetStringUTFChars(hostAddress_, 0);
    const char *remoteFile = env->GetStringUTFChars(remoteFile_, 0);
    const char *localFile = env->GetStringUTFChars(localFile_, 0);

//    LOGE("hostAddress=%s, remoteFile=%s, localFile=%s", hostAddress, remoteFile, localFile);

    int result = download(const_cast<char *>(hostAddress), const_cast<char *>(remoteFile),
             const_cast<char *>(localFile));

    env->ReleaseStringUTFChars(hostAddress_, hostAddress);
    env->ReleaseStringUTFChars(remoteFile_, remoteFile);
    env->ReleaseStringUTFChars(localFile_, localFile);

    return result;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_xuexiang_tftp_TFTPClientUtils_upload(JNIEnv *env, jclass type, jstring hostAddress_,
                                         jstring remoteFile_, jstring localFile_) {
    const char *hostAddress = env->GetStringUTFChars(hostAddress_, 0);
    const char *remoteFile = env->GetStringUTFChars(remoteFile_, 0);
    const char *localFile = env->GetStringUTFChars(localFile_, 0);

    int result = upload(const_cast<char *>(hostAddress), const_cast<char *>(remoteFile),
                          const_cast<char *>(localFile));

    env->ReleaseStringUTFChars(hostAddress_, hostAddress);
    env->ReleaseStringUTFChars(remoteFile_, remoteFile);
    env->ReleaseStringUTFChars(localFile_, localFile);

    return result;
}